package com.yourenbang.lingyun.transaction.web.service.impl.merchant;

import com.yourenbang.lingyun.constants.enums.ChannelIdEnums;
import com.yourenbang.lingyun.constants.enums.PlatNoEnums;
import com.yourenbang.lingyun.core.util.BizAssertUtils;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.core.util.date.DateFormat;
import com.yourenbang.lingyun.domain.*;
import com.yourenbang.lingyun.mapper.UserMerchantRecommend20Mapper;
import com.yourenbang.lingyun.mapper.UserMerchantRecommend21Mapper;
import com.yourenbang.lingyun.mapper.UserMerchantRecommendChannel20Mapper;
import com.yourenbang.lingyun.mapper.UserMerchantRecommendChannel21Mapper;
import com.yourenbang.lingyun.transaction.web.service.merchant.MerchantRecommendService;
import com.yourenbang.ucenter.api.model.dto.MerchantStatisticsDTO;
import com.yourenbang.ucenter.api.service.MerchantRpcService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service("merchantRecommendService")
@Slf4j
public class MerchantRecommendServiceImpl implements MerchantRecommendService {

    @Autowired
    private UserMerchantRecommend20Mapper recommend20Mapper;
    @Autowired
    private UserMerchantRecommend21Mapper recommend21Mapper;
    @Autowired
    private UserMerchantRecommendChannel20Mapper recommendChannel20Mapper;
    @Autowired
    private UserMerchantRecommendChannel21Mapper recommendChannel21Mapper;
    @Autowired
    private MerchantRpcService merchantRpcService;

    /**
     * 更新电签商户标识-快收
     */
    @Override
    public void updateEposMerFlag(String userBaseId, Short channelId) {
        UserMerchantRecommend20Example recommend20Example = new UserMerchantRecommend20Example();
        recommend20Example.createCriteria()
                .andUserBaseIdEqualTo(userBaseId);
        List<UserMerchantRecommend20> recommend20s = recommend20Mapper.selectByExample(recommend20Example);
        if (CollectionUtils.isEmpty(recommend20s)) {
            return;
        }
        UserMerchantRecommend20 recommend20 = recommend20s.get(0);
        //不是电签商户
        if (ChannelIdEnums.epos_p30.isEquals(channelId)) {
            UserMerchantRecommendChannel20Example recommendChannel20Example = new UserMerchantRecommendChannel20Example();
            recommendChannel20Example.createCriteria()
                    .andUserMerchantRecommendIdEqualTo(recommend20.getUserMerchantRecommendId())
                    .andChannelIdEqualTo(ChannelIdEnums.epos_p30.getCode());
            List<UserMerchantRecommendChannel20> recommendChannel20s = recommendChannel20Mapper.selectByExample(recommendChannel20Example);
            if (CollectionUtils.isEmpty(recommendChannel20s)) {
                UserMerchantRecommendChannel20 inRecommendChannel = new UserMerchantRecommendChannel20();
                inRecommendChannel.setUserBaseId(userBaseId);
                inRecommendChannel.setUserMerchantRecommendChannelId(recommend20.getUserMerchantRecommendId());
                inRecommendChannel.setBecomeMerchantTime(LocalDateTime.now());
                inRecommendChannel.setChannelId(ChannelIdEnums.epos_p30.getCode());
                int inRecount = recommendChannel20Mapper.insertSelective(inRecommendChannel);
                BizAssertUtils.assertOne(inRecount, "添加商户类型失败");
            }
        } else if (ChannelIdEnums.epos.isEquals(channelId)) {
            UserMerchantRecommendChannel20Example recommendChannel20Example = new UserMerchantRecommendChannel20Example();
            recommendChannel20Example.createCriteria()
                    .andUserMerchantRecommendIdEqualTo(recommend20.getUserMerchantRecommendId())
                    .andChannelIdEqualTo(ChannelIdEnums.epos.getCode());
            List<UserMerchantRecommendChannel20> recommendChannel20s = recommendChannel20Mapper.selectByExample(recommendChannel20Example);
            if (CollectionUtils.isEmpty(recommendChannel20s)) {
                UserMerchantRecommendChannel20 inRecommendChannel = new UserMerchantRecommendChannel20();
                inRecommendChannel.setUserBaseId(userBaseId);
                inRecommendChannel.setUserMerchantRecommendChannelId(recommend20.getUserMerchantRecommendId());
                inRecommendChannel.setBecomeMerchantTime(LocalDateTime.now());
                inRecommendChannel.setChannelId(ChannelIdEnums.epos.getCode());
                int inRecount = recommendChannel20Mapper.insertSelective(inRecommendChannel);
                BizAssertUtils.assertOne(inRecount, "添加商户类型失败");
            }
        } else {
            return;
        }

        statisticsMerchantNumber(userBaseId, channelId);
    }
    /**
     * 更新电签商户标识-银小云
     */
    @Override
    public void updateEposMerFlagYxy(String userBaseId, Short channelId) {
        UserMerchantRecommend21Example recommend21Example = new UserMerchantRecommend21Example();
        recommend21Example.createCriteria()
                .andUserBaseIdEqualTo(userBaseId);
        List<UserMerchantRecommend21> recommend21s = recommend21Mapper.selectByExample(recommend21Example);
        if (CollectionUtils.isEmpty(recommend21s)) {
            return;
        }
        UserMerchantRecommend21 recommend21 = recommend21s.get(0);
        KLog.info("查到的UserMerchantRecommend21:{}", recommend21.toString());
            UserMerchantRecommendChannel21Example recommendChannel21Example = new UserMerchantRecommendChannel21Example();
            recommendChannel21Example.createCriteria()
                    .andUserMerchantRecommendIdEqualTo(recommend21.getUserMerchantRecommendId())
                    .andChannelIdEqualTo(channelId);
            List<UserMerchantRecommendChannel21> recommendChannel21s = recommendChannel21Mapper.selectByExample(recommendChannel21Example);
            if (CollectionUtils.isEmpty(recommendChannel21s)) {
                UserMerchantRecommendChannel21 inRecommendChannel = new UserMerchantRecommendChannel21();
                inRecommendChannel.setUserBaseId(userBaseId);
                inRecommendChannel.setUserMerchantRecommendId(recommend21.getUserMerchantRecommendId());
                inRecommendChannel.setBecomeMerchantTime(LocalDateTime.now());
                inRecommendChannel.setChannelId(channelId);
                inRecommendChannel.setCreateTime(LocalDateTime.now());
                int inRecount = recommendChannel21Mapper.insert(inRecommendChannel);
                BizAssertUtils.assertOne(inRecount, "添加商户类型失败");
            }

    }

    /**
     * 更新P35商户标识
     */
    @Override
    public void updateP35MerFlag(String userBaseId,Short channelId) {
        UserMerchantRecommend20Example recommend20Example = new UserMerchantRecommend20Example();
        recommend20Example.createCriteria()
                .andUserBaseIdEqualTo(userBaseId);
        List<UserMerchantRecommend20> recommend20s = recommend20Mapper.selectByExample(recommend20Example);
        if (CollectionUtils.isEmpty(recommend20s)) {
            return;
        }
        UserMerchantRecommend20 recommend20 = recommend20s.get(0);
        UserMerchantRecommend20 upRecomend = new UserMerchantRecommend20();
        upRecomend.setUserMerchantRecommendId(recommend20.getUserMerchantRecommendId());
        upRecomend.setActivateOrSignStatus(Short.valueOf("1"));
        upRecomend.setActivateOrSignTime(LocalDateTime.now());
        int upRecomendCc = recommend20Mapper.updateByPrimaryKeySelective(upRecomend);
        BizAssertUtils.assertOne(upRecomendCc, "更新商户信息失败");

        UserMerchantRecommendChannel20Example recommendChannel20Example = new UserMerchantRecommendChannel20Example();
        recommendChannel20Example.createCriteria()
                .andUserMerchantRecommendIdEqualTo(recommend20.getUserMerchantRecommendId())
                .andChannelIdEqualTo(channelId);
        List<UserMerchantRecommendChannel20> recommendChannel20s = recommendChannel20Mapper.selectByExample(recommendChannel20Example);
        if (CollectionUtils.isEmpty(recommendChannel20s)) {
            UserMerchantRecommendChannel20 inRecommendChannel = new UserMerchantRecommendChannel20();
            inRecommendChannel.setUserBaseId(userBaseId);
            inRecommendChannel.setUserMerchantRecommendChannelId(recommend20.getUserMerchantRecommendId());
            inRecommendChannel.setBecomeMerchantTime(LocalDateTime.now());
            inRecommendChannel.setChannelId(channelId);
            int inRecount = recommendChannel20Mapper.insertSelective(inRecommendChannel);
            BizAssertUtils.assertOne(inRecount, "添加商户类型失败");
        }

        statisticsMerchantNumber(userBaseId, channelId);
    }

    /**
     * 实时统计商户数
     */
    private void statisticsMerchantNumber(String userBaseId, Short channelId){
        MerchantStatisticsDTO statisticsDTO = new MerchantStatisticsDTO();
        statisticsDTO.setUserBaseId(userBaseId);
        statisticsDTO.setPlatNo(PlatNoEnums.KLKS.getCode());
        statisticsDTO.setChannelId(channelId);
        statisticsDTO.setBecomeMerchantTime(DateFormat.DATETIME_DEFAULT.format(LocalDateTime.now()));
        merchantRpcService.statisticsMerchantNumberForOne(statisticsDTO);
    }
}
